﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATM_Software
{
    // This is a data object to contain a customer and part of Bussines tier
    public class CustomerData
    {
        // Auto implemented properties of Customer
        public int UserId { get; set; }
        public int AccountId{ get; set; }        
        public string Login { get; set; }
        public int? PinCode { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public int? Balance { get; set; }
        public string Status { get; set; }

        public CustomerData() // default constructor
        {
            UserId = AccountId = 0;
            Balance = null;
            PinCode = null;
            Login = Name = Type = Status = "";
        }
        // construct new object and initialize the values with the values of incoming object
        public CustomerData(CustomerData obj) 
        {
            UserId = obj.UserId;
            AccountId = obj.AccountId;
            Balance = obj.Balance;
            PinCode = obj.PinCode;
            Login = obj.Login;
            Name = obj.Name;
            Type = obj.Type;
            Status = obj.Status;
        }
        public override string ToString()
        {
            return "Account # : " + AccountId.ToString()+ "\nType : " + Type +
                    "\nHolder Name : " + Name + "\nBalance : " + Balance.ToString() +
                    "\nStatus : " + Status;
        }
        public string ToAccountString()
        {
            return UserId.ToString() + ";" +
                AccountId.ToString() + ";" + Name + ";" +
                Type + ";" + Balance.ToString() + ";" + Status;
        }
        public string ToLoginString()
        {
            return UserId.ToString() + ";" + Login + ";" + PinCode.ToString();                
        }
        public bool isDefaultObject()
        {
            if (UserId==0 && AccountId==0 && Login.Equals("") &&
                PinCode==null && Name.Equals("") && Type.Equals("") && Balance==null && Status.Equals(""))
	        {
		        return true;
	        }
            return false;
        }
        
        public void EncryptDecrypt()
        {
            // encrypting login

            char[] encryptedLogin = this.Login.ToCharArray();
            int counter = 0;
            foreach (char item in encryptedLogin)
            {
                if (item >= 65 && item <= 90) // encrypt Capital letters
                {
                    encryptedLogin[counter] = (char)(65 + (90 - item));
                }
                else if (item >= 97 && item <= 122) // encrypt Small letters
                {
                    encryptedLogin[counter] = (char)(97 + (122 - item));
                }
                else if (item >= 48 && item <= 57) // encrypt digits
                {
                    encryptedLogin[counter] = (char)(48 + (57 - item));
                }
                counter++;
            }
            this.Login = new string(encryptedLogin); // coverting char array to string

            //encryptin pin code

            int pin = (int)this.PinCode; // because PinCode can accept null
            int remainder = 0;
            string encryptedPin = "";
            while (pin>=10)
            {
                remainder = pin%10;
                pin = pin / 10;
                encryptedPin += (9 - remainder).ToString();
            }
            encryptedPin += (9 - pin).ToString();
            char[] reversed = encryptedPin.ToCharArray();
            reversed = reversed.Reverse().ToArray();
            encryptedPin = new string(reversed); // coverting char array to string
            this.PinCode = int.Parse(encryptedPin);                        
        }
    }
}
